/* -----------------------------------------------------------------------------
 * PROJECT    : net.lwf
 * FILE       : fFindTimeHandler.java
 *
 * HISTORY:
 * Date          Author                        Description
 * ----------------------------------------------------------------------------- 
 * Jan 19, 2009       Raghu Sarraff			       Created
 */
package net.lwf.handler.time;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.StringTokenizer;

import lwf.handler.IProcessHandler;
import lwf.handler.ProcessHandler;
import lwf.handler.entities.RadioData;
import lwf.handler.helper.DisplayFormatter;
import net.lwf.common.constant.ICommonConstant;
import net.lwf.entity.QuestionInfo;
import net.lwf.handler.constant.IHandlerConstant;
import net.lwf.handler.time.util.Time;
import net.lwf.handler.time.util.TimeGenerator;
import net.lwf.number.util.RandomNumberGenerator;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;

public class FindTimeHandler extends ProcessHandler implements IProcessHandler {
	
	private Time time = null;
	
	public FindTimeHandler() {
		init();
	}
	
	
	/**
	 * @see net.lwf.handler.IProcessHandler#getDisplayFormatter()
	 */
	public DisplayFormatter getDisplayFormatter() {
		return null;
	}
	
	private void init() {
		while(this.time == null || this.time.getHours() == 0) {
			int type = RandomNumberGenerator.generate(1,2);
			if(type == 1) {
				//Minutes are multiples of 5
				int multiple = RandomNumberGenerator.generate(1, IHandlerConstant.EASY_MULTIPLES.length);
				int minutemultiple = IHandlerConstant.EASY_MULTIPLES[multiple-1];
				this.time = TimeGenerator.generateHMM(minutemultiple);
			}
			else {
				this.time = TimeGenerator.generateHMM();
			}
		}
	}
	
	private String generateHtmlQuestion() {
		StringBuffer sbQBlock = new StringBuffer();
		sbQBlock.append("<table border=\"0\" align=\"center\" cellspacing=\"2\" cellpadding=\"0\">");
//			sbQBlock.append("<tr>");
//				sbQBlock.append("<td> &nbsp; </td>");
//		    sbQBlock.append("</tr>");
        	sbQBlock.append("<tr>");
       			sbQBlock.append("<td align=\"center\">");
       			    sbQBlock.append("<div class=\"time_clock\" align=\"center\" >");
       					sbQBlock.append("<img src=\"" + this.time.getImagePath() + "\" >");
       				sbQBlock.append("</div>");
	        	sbQBlock.append("</td>");
        	sbQBlock.append("</tr>");
//			sbQBlock.append("<tr>");
//				sbQBlock.append("<td> &nbsp; </td>");
//			sbQBlock.append("</tr>");
        sbQBlock.append("</table>");
        
        return sbQBlock.toString();	
	}
	
	/**
	 * @see net.lwf.handler.IProcessHandler#getQuestionDesc()
	 */
	public String getQuestionDesc() {
		return "The generated time is: " + getAnswer() + "<br> Find the time from the picture?";
	}
	
	//------------------------------------------------ Methods for PDF generation on the browser
	public void generate(Document document) {
	
	}

	@Override
	public List<QuestionInfo> generateMultiple() {
		return null;
	}

	@Override
	public QuestionInfo generateSingle() {
		return this.generateSingle(false);
	}
	
	@Override
	public int getColumns() {
		return 0;
	}

	@Override
	public void getContent(Document document) throws DocumentException {
	}

	@Override
	public int getRows() {
		return 0;
	}

	@Override
	public boolean isPortrait() {
		return false;
	}


	@Override
	public String getPdfInstructionalText() {
		return null;
	}

	@Override
	public String getPdfNote() {
		return null;
	}

	private QuestionInfo generateSingle(boolean isMultiple) {
		
		QuestionInfo qinfo = new QuestionInfo();
		//Set answer
        String answer = ICommonConstant.EMPTY;
        
        if(this.time.getHours() > 12) {
        	answer += String.valueOf(this.time.getHours() - 12);
        }
        else {
        	answer += String.valueOf(this.time.getHours());
        }
        answer += ICommonConstant.COLON;
        answer += String.valueOf(this.time.getMinutes());
        qinfo.setAnswer(answer);
        //Set Question
        qinfo.setHtmlQuestion(this.generateHtmlQuestion());
        //Set instruction
        qinfo.setPlaceHolder("What is the time in the clock below?");
        
        //Set radio answers
		qinfo.setRadioAnswerChoices(this.getRadioAnswers(answer));
		
		return qinfo;
	}
	
	private List<RadioData> getRadioAnswers(String answer) {
		List<RadioData> rdata = new ArrayList<RadioData>();
		rdata.add(new RadioData(answer, answer));
		StringTokenizer stk = new StringTokenizer(answer, ICommonConstant.COLON);
		
		int hour = Integer.parseInt(stk.nextToken());
		int minute = Integer.parseInt(stk.nextToken());
		
		List<Integer> hour_choices = RandomNumberGenerator.generateIntsWithVariance(hour, 12, 3, 3);
		List<Integer> minute_choices = RandomNumberGenerator.generateIntsWithVariance(minute, 60, 3, 3);
		
		for(int i=0; i<hour_choices.size(); i++) {
			String hour_choice = String.valueOf(hour_choices.get(i)); 
			String minute_choice = String.valueOf(minute_choices.get(i)); 
			String answer_choice = hour_choice + ICommonConstant.COLON + minute_choice;
			rdata.add(new RadioData(answer_choice, answer_choice));
		}
		
		Collections.shuffle(rdata);
		
		return rdata;
	}
}

